package com.zp.business.impl.prescription;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.api.RemoteAdministrationDictService;
import com.zp.base.api.vo.PerformFreqDictVo;
import com.zp.business.api.prescription.PrescriptionApply;
import com.zp.business.api.prescription.PrescriptionItem;
import com.zp.business.api.prescription.bo.PrescriptionApplyBo;
import com.zp.business.api.prescription.bo.PrescriptionItemBo;
import com.zp.business.api.prescription.vo.DistributeMedicationVo;
import com.zp.business.api.prescription.vo.PrescriptionApplyVo;
import com.zp.business.api.prescription.vo.PrescriptionItemVo;
import com.zp.business.entity.clinicOrder.ClinicOrders;
import com.zp.business.entity.clinicOrder.ClinicOrdersCost;
import com.zp.business.entity.outpFees.OutpFeesDetail;
import com.zp.business.mapper.clinicOrder.ClinicOrdersCostMapper;
import com.zp.business.mapper.clinicOrder.ClinicOrdersMapper;
import com.zp.business.mapper.outpFees.OutpFeesDetailMapper;
import com.zp.business.mapper.prescription.PrescriptionApplyMapper;
import com.zp.business.mapper.prescription.PrescriptionItemMapper;
import com.zp.business.service.prescription.IPrescriptionItemService;
import com.zp.common.core.constant.BaseConstants;
import com.zp.common.core.constant.CacheNames;
import com.zp.common.core.utils.DateUtils;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.redis.utils.CacheUtils;
import com.zp.common.satoken.utils.LoginHelper;
import com.zp.drug.api.RemoteDrugStockService;
import com.zp.drug.api.vo.DrugInfoVo;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 处方子prescription_itemService业务层处理
 *
 * @author zhang peng
 * @ date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class PrescriptionItemServiceImpl implements IPrescriptionItemService {

    private final PrescriptionItemMapper baseMapper;
    private final PrescriptionItemMapper prescriptionItemMapper;
    private final ClinicOrdersMapper clinicOrdersMapper;
    private final ClinicOrdersCostMapper clinicOrdersCostMapper;
    private final OutpFeesDetailMapper outpFeesDetailMapper;
    private final PrescriptionApplyMapper prescriptionApplyMapper;

    static Map<String, PerformFreqDictVo> performFreqDictMap = CacheUtils.get(CacheNames.PERFORM_FREQ_DICT);

    // 药品基本信息
    static Map<String, DrugInfoVo> drugInfoVoMap = CacheUtils.get(CacheNames.DRUG_INFO);
    @DubboReference
    private RemoteDrugStockService remoteDrugStockService;
    @DubboReference
    private RemoteAdministrationDictService administrationDictService;

    /**
     * 查询处方子prescription_item
     */
    @Override
    public PrescriptionItemVo queryById(String itemId) {
        return baseMapper.selectVoById(itemId);
    }

    /**
     * 查询处方子prescription_item列表(分页)
     */
    @Override
    public TableDataInfo<PrescriptionItemVo> queryPageList(PrescriptionItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PrescriptionItem> lqw = buildQueryWrapper(bo);
        Page<PrescriptionItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(item -> {
            item.setPerformFreqName(StringUtils.isNotBlank(item.getPerformFreq()) && CollectionUtil.isNotEmpty(performFreqDictMap)
                    ? performFreqDictMap.get(item.getPerformFreq()).getFreqDesc() : "");
            String administrationLabel = administrationDictService.getAdministrationLabel(bo.getOrgId(), item.getChannel());
            item.setChannelName(administrationLabel);
            item.setSingleDosageMin(CollectionUtil.isNotEmpty(drugInfoVoMap) ?
                    drugInfoVoMap.get(item.getDrugInfoId()).getDose() : BigDecimal.valueOf(0.00));
        });
        return TableDataInfo.build(result);
    }


    /**
     * 查询处方子prescription_item列表(不分页)
     */
    @Override
    public List<PrescriptionItemVo> queryList(PrescriptionItemBo bo) {
        LambdaQueryWrapper<PrescriptionItem> lqw = buildQueryWrapper(bo);

        List<PrescriptionItemVo> prescriptionItemVos = baseMapper.selectVoList(lqw);
        prescriptionItemVos.forEach(item -> {
            item.setPerformFreqName(StringUtils.isNotBlank(item.getPerformFreq()) && CollectionUtil.isNotEmpty(performFreqDictMap)
                    ? performFreqDictMap.get(item.getPerformFreq()).getFreqDesc() : "");
            String administrationLabel = administrationDictService.getAdministrationLabel(bo.getOrgId(), item.getChannel());
            item.setChannelName(administrationLabel);
            item.setSingleDosageMin(CollectionUtil.isNotEmpty(drugInfoVoMap) ?
                    drugInfoVoMap.get(item.getDrugInfoId()).getDose() : BigDecimal.valueOf(0.00));
            item.setPackageNum(CollectionUtil.isNotEmpty(drugInfoVoMap) ?
                    drugInfoVoMap.get(item.getDrugInfoId()).getPackageNum() : 2);
            item.setPerformFreqInfo(performFreqDictMap.get(item.getPerformFreq()));
        });
        return prescriptionItemVos;
    }

    private LambdaQueryWrapper<PrescriptionItem> buildQueryWrapper(PrescriptionItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PrescriptionItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getItemId()), PrescriptionItem::getItemId, bo.getItemId());
        lqw.eq(PrescriptionItem::getDelFlag, "0");
        lqw.eq(ObjectUtil.isNotNull(bo.getGroupNumber()), PrescriptionItem::getGroupNumber, bo.getGroupNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getApplyId()), PrescriptionItem::getApplyId, bo.getApplyId());
        lqw.eq(StringUtils.isNotBlank(bo.getPriceId()), PrescriptionItem::getPriceId, bo.getPriceId());
        lqw.eq(StringUtils.isNotBlank(bo.getIdenticalInfoId()), PrescriptionItem::getIdenticalInfoId, bo.getIdenticalInfoId());
        lqw.eq(StringUtils.isNotBlank(bo.getStockId()), PrescriptionItem::getStockId, bo.getStockId());
        lqw.like(StringUtils.isNotBlank(bo.getDrugName()), PrescriptionItem::getDrugName, bo.getDrugName());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugCode()), PrescriptionItem::getDrugCode, bo.getDrugCode());
        lqw.eq(StringUtils.isNotBlank(bo.getMinSpec()), PrescriptionItem::getMinSpec, bo.getMinSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageUnits()), PrescriptionItem::getPackageUnits, bo.getPackageUnits());
        lqw.eq(bo.getSort() != null, PrescriptionItem::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getMinUnits()), PrescriptionItem::getMinUnits, bo.getMinUnits());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageSpec()), PrescriptionItem::getPackageSpec, bo.getPackageSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getFirmId()), PrescriptionItem::getFirmId, bo.getFirmId());
        lqw.like(StringUtils.isNotBlank(bo.getFirmName()), PrescriptionItem::getFirmName, bo.getFirmName());
        lqw.eq(bo.getSingleDosage() != null, PrescriptionItem::getSingleDosage, bo.getSingleDosage());
        lqw.eq(StringUtils.isNotBlank(bo.getSingleUnit()), PrescriptionItem::getSingleUnit, bo.getSingleUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getChannel()), PrescriptionItem::getChannel, bo.getChannel());
        lqw.eq(StringUtils.isNotBlank(bo.getPerformFreq()), PrescriptionItem::getPerformFreq, bo.getPerformFreq());
        lqw.eq(bo.getNum() != null, PrescriptionItem::getNum, bo.getNum());
        lqw.eq(bo.getNumSingle() != null, PrescriptionItem::getNumSingle, bo.getNumSingle());
        lqw.eq(bo.getNetworkPrice() != null, PrescriptionItem::getNetworkPrice, bo.getNetworkPrice());
        lqw.eq(bo.getPrice() != null, PrescriptionItem::getPrice, bo.getPrice());
        lqw.eq(bo.getTradePrice() != null, PrescriptionItem::getTradePrice, bo.getTradePrice());
        lqw.eq(bo.getSumPrice() != null, PrescriptionItem::getSumPrice, bo.getSumPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getIsSkin()), PrescriptionItem::getIsSkin, bo.getIsSkin());
        lqw.eq(StringUtils.isNotBlank(bo.getNoSkin()), PrescriptionItem::getNoSkin, bo.getNoSkin());
        lqw.eq(StringUtils.isNotBlank(bo.getVerifyFlag()), PrescriptionItem::getVerifyFlag, bo.getVerifyFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getSkinResult()), PrescriptionItem::getSkinResult, bo.getSkinResult());
        lqw.eq(bo.getAbidance() != null, PrescriptionItem::getAbidance, bo.getAbidance());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), PrescriptionItem::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), PrescriptionItem::getRemarks, bo.getRemarks());
        lqw.eq(StringUtils.isNotBlank(bo.getSkinConfirmBy()), PrescriptionItem::getSkinConfirmBy, bo.getSkinConfirmBy());
        lqw.eq(StringUtils.isNotBlank(bo.getSkinPerson()), PrescriptionItem::getSkinPerson, bo.getSkinPerson());
        lqw.eq(bo.getConfirmTime() != null, PrescriptionItem::getConfirmTime, bo.getConfirmTime());
        lqw.eq(bo.getSkinTime() != null, PrescriptionItem::getSkinTime, bo.getSkinTime());
        lqw.eq(bo.getDoseUnitsMetering() != null, PrescriptionItem::getDoseUnitsMetering, bo.getDoseUnitsMetering());
        lqw.eq(StringUtils.isNotBlank(bo.getExecutiveDepartment()), PrescriptionItem::getExecutiveDepartment, bo.getExecutiveDepartment());
        lqw.eq(StringUtils.isNotBlank(bo.getSkinId()), PrescriptionItem::getSkinId, bo.getSkinId());
        lqw.eq(StringUtils.isNotBlank(bo.getSkinResults()), PrescriptionItem::getSkinResults, bo.getSkinResults());
        lqw.eq(StringUtils.isNotBlank(bo.getDripSpeed()), PrescriptionItem::getDripSpeed, bo.getDripSpeed());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), PrescriptionItem::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getWarnCode() != null, PrescriptionItem::getWarnCode, bo.getWarnCode());
        lqw.orderByAsc(PrescriptionItem::getGroupNumber).orderByAsc(PrescriptionItem::getSort);
        return lqw;
    }

    /**
     * 新增处方子prescription_item
     */
    @Override
    public Boolean insertByBo(PrescriptionItemBo bo) {
        PrescriptionItem add = BeanUtil.toBean(bo, PrescriptionItem.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setItemId(add.getItemId());
        }
        return flag;
    }

    /**
     * 修改处方子prescription_item
     */
    @Override
    public Boolean updateByBo(PrescriptionItemBo bo) {
        PrescriptionItem update = BeanUtil.toBean(bo, PrescriptionItem.class);
        update.setUpdateBy(LoginHelper.getLoginUser().getNickName());
        update.setUpdateTime(new Date());
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PrescriptionItem entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除处方子prescription_item
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional
    public List<PrescriptionItem> saveBatch(List<PrescriptionItemBo> list) {
        List<PrescriptionItem> prescriptionItems = BeanUtil.copyToList(list, PrescriptionItem.class);
        baseMapper.insertBatch(prescriptionItems);
        return prescriptionItems;
    }


    /**
     * 根据applyId删除处方明细
     *
     * @param applyId
     * @return
     */
    @Override
    public Boolean deleteByApplyId(String applyId) {
        return baseMapper.delete(Wrappers.<PrescriptionItem>lambdaQuery()
                .eq(PrescriptionItem::getApplyId, applyId)) > 0;
    }

    /**
     * 查询子列表map
     *
     * @param bo
     * @return
     */
    @Override
    public Map<String, Object> listPrescriptionItemMap(PrescriptionItemBo bo) {
        Map<String, Object> map = new HashMap<>();
        String[] applyIds = bo.getApplyIds();
        // new 一个新的发药页面展示实体list
        List<DistributeMedicationVo> distributeMedicationVos = new ArrayList<>();
        if (applyIds != null && applyIds.length > 0) {
            List<PrescriptionApplyVo> prescriptionApplyVos = getPrescriptionApply(applyIds, bo.getOrgId());
            // 查询明细
            List<PrescriptionItemVo> prescriptionItemVoList = baseMapper.selectVoList(Wrappers.<PrescriptionItem>lambdaQuery()
                    .in(PrescriptionItem::getApplyId, Arrays.asList(applyIds))
            );
            prescriptionApplyVos.forEach(apply -> {
                // new 一个新的发药页面展示实体
                DistributeMedicationVo distributeMedicationVo = new DistributeMedicationVo();
                String key = "领药人：" + apply.getName() + " -- 年龄：" + apply.getAge() + "岁" + apply.getAgeMonth() + "月" +
                        " -- 处方号：" + apply.getApplyNo() + " -- 诊断：" + apply.getIcdName();
                distributeMedicationVo.setKey(key);
                distributeMedicationVo.setPrescriptionApplyVo(apply);
                List<PrescriptionItemVo> collect = prescriptionItemVoList.stream()
                        .filter(item -> StringUtils.equals(apply.getApplyId(), item.getApplyId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(collect)) {
                    collect.forEach(item -> {
                        item.setPerformFreqName(StringUtils.isNotBlank(item.getPerformFreq()) && CollectionUtil.isNotEmpty(performFreqDictMap)
                                ? performFreqDictMap.get(item.getPerformFreq()).getFreqDesc() : "");
                        String administrationLabel = administrationDictService.getAdministrationLabel(bo.getOrgId(), item.getChannel());
                        item.setChannelName(administrationLabel);
                        item.setSingleDosageMin(CollectionUtil.isNotEmpty(drugInfoVoMap) ?
                                drugInfoVoMap.get(item.getDrugInfoId()).getDose() : BigDecimal.valueOf(0.00));
                        item.setPackageNum(CollectionUtil.isNotEmpty(drugInfoVoMap) ?
                                drugInfoVoMap.get(item.getDrugInfoId()).getPackageNum() : 2);
                        item.setPerformFreqInfo(performFreqDictMap.get(item.getPerformFreq()));
                    });
                    distributeMedicationVo.setPrescriptionItemVoList(collect);
                }
                distributeMedicationVos.add(distributeMedicationVo);
            });
            map.put("list", distributeMedicationVos);
            map.put("summary", baseMapper.getSummary(bo));
        }
        return map;
    }

    /**
     * 获取主记录信息
     *
     * @param applyIds 主记录id 数组
     * @param orgId    机构id
     * @return List<PrescriptionApplyVo>
     */
    private List<PrescriptionApplyVo> getPrescriptionApply(String[] applyIds, String orgId) {
        // 查询主记录
        PrescriptionApplyBo prescriptionApplyBo = new PrescriptionApplyBo();
        prescriptionApplyBo.setApplyIds(applyIds);
        prescriptionApplyBo.setOrgId(orgId);
        return prescriptionApplyMapper.selectVoPage(prescriptionApplyBo);
    }

    /**
     * 确认发放
     *
     * @param bo
     * @return
     */
    @Override
    @GlobalTransactional
    public Boolean grantInfoOk(PrescriptionItemBo bo) {
        Boolean b = false;
        String[] applyIds = bo.getApplyIds();
        if (applyIds != null && applyIds.length > 0) {
            List<PrescriptionApplyVo> prescriptionApplyVos = getPrescriptionApply(applyIds, bo.getOrgId());
            List<String> orderIds = prescriptionApplyVos.stream().map(PrescriptionApplyVo::getOrdersId).collect(Collectors.toList());
            // 查询明细
            List<PrescriptionItemVo> prescriptionItemVoList = baseMapper.selectVoList(Wrappers.<PrescriptionItem>lambdaQuery()
                    .in(PrescriptionItem::getApplyId, Arrays.asList(applyIds))
            );
            prescriptionApplyVos.forEach(apply -> {
                List<PrescriptionItemVo> collect = prescriptionItemVoList.stream()
                        .filter(item -> StringUtils.equals(apply.getApplyId(), item.getApplyId())).collect(Collectors.toList());
                apply.setPrescriptionItemVos(collect);
            });
            b = remoteDrugStockService.grantInfoOk(prescriptionApplyVos);

            if (b) {
                prescriptionApplyMapper.update(null, Wrappers.<PrescriptionApply>lambdaUpdate()
                        .set(PrescriptionApply::getConfirmFlag, BaseConstants.CONFIRM_FLAG.ISSUED)
                        .set(PrescriptionApply::getImplementUser, LoginHelper.getUserId())
                        .set(PrescriptionApply::getGrantTime, DateUtils.getNowDate())
                        .set(PrescriptionApply::getUpdateBy, LoginHelper.getLoginUser().getNickName())
                        .set(PrescriptionApply::getUpdateTime, DateUtils.getNowDate())
                        .in(PrescriptionApply::getApplyId, Arrays.asList(applyIds))
                );
                prescriptionItemMapper.update(null, Wrappers.<PrescriptionItem>lambdaUpdate()
                        .set(PrescriptionItem::getConfirmFlag, BaseConstants.CONFIRM_FLAG.ISSUED)
                        .in(PrescriptionItem::getApplyId, Arrays.asList(applyIds))
                );
                clinicOrdersMapper.update(null, Wrappers.<ClinicOrders>lambdaUpdate()
                        .set(ClinicOrders::getConfirmFlag, BaseConstants.CONFIRM_FLAG.ISSUED)
                        .in(ClinicOrders::getId, orderIds)
                );
                clinicOrdersCostMapper.update(null, Wrappers.<ClinicOrdersCost>lambdaUpdate()
                        .set(ClinicOrdersCost::getConfirmFlag, BaseConstants.CONFIRM_FLAG.ISSUED)
                        .in(ClinicOrdersCost::getOrdersId, orderIds)
                );
                List<String> applyItemIds = prescriptionItemVoList.stream().map(PrescriptionItemVo::getItemId).collect(Collectors.toList());
                outpFeesDetailMapper.update(null, Wrappers.<OutpFeesDetail>lambdaUpdate()
                        .set(OutpFeesDetail::getConfirmFlag, BaseConstants.CONFIRM_FLAG.ISSUED)
                        .in(OutpFeesDetail::getApplyItemId, applyItemIds)
                );
            }
        }
        return b;
    }

    /**
     * 确认退费
     *
     * @param bo
     * @return
     */
    @Override
    @GlobalTransactional
    public Boolean issuedGrantInfoOk(PrescriptionItemBo bo) {
        String applyId = bo.getApplyId();
        PrescriptionApplyVo prescriptionApplyVo = prescriptionApplyMapper.selectVoOne(Wrappers.<PrescriptionApply>lambdaQuery()
                .eq(PrescriptionApply::getApplyId, applyId)
        );
        List<PrescriptionItem> prescriptionItems = baseMapper.selectList(Wrappers.<PrescriptionItem>lambdaQuery()
                .eq(PrescriptionItem::getApplyId, applyId)
        );
        prescriptionApplyVo.setPrescriptionItemVos(BeanUtil.copyToList(prescriptionItems, PrescriptionItemVo.class));
        if (!StringUtils.equals(BaseConstants.CHARGE_FLAG_DICT.SIX, prescriptionApplyVo.getChargeFlag())) {
            throw new RuntimeException("处方费用状态已改变，不能退药，请刷新页面后再退药。");
        }
        // 确认退药：库存恢复方法
        Boolean b = remoteDrugStockService.issuedGrantInfoOk(prescriptionApplyVo);
        if (b) {
            prescriptionApplyMapper.update(null, Wrappers.<PrescriptionApply>lambdaUpdate()
                    .set(PrescriptionApply::getChargeFlag, BaseConstants.CHARGE_FLAG_DICT.THREE)
                    .set(PrescriptionApply::getConfirmFlag, BaseConstants.CONFIRM_FLAG.RETURNED)
                    .set(PrescriptionApply::getReturnDrugTime, DateUtils.getNowDate())
                    .set(PrescriptionApply::getReturnDrugUser, LoginHelper.getLoginUser().getNickName())
                    .set(PrescriptionApply::getUpdateTime, DateUtils.getNowDate())
                    .eq(PrescriptionApply::getApplyId, prescriptionApplyVo.getApplyId())
            );
            prescriptionItemMapper.update(null, Wrappers.<PrescriptionItem>lambdaUpdate()
                    .set(PrescriptionItem::getChargeFlag, BaseConstants.CHARGE_FLAG_DICT.THREE)
                    .set(PrescriptionItem::getConfirmFlag, BaseConstants.CONFIRM_FLAG.RETURNED)
                    .eq(PrescriptionItem::getApplyId, prescriptionApplyVo.getApplyId())
            );
            clinicOrdersMapper.update(null, Wrappers.<ClinicOrders>lambdaUpdate()
                    .set(ClinicOrders::getChargeFlag, BaseConstants.CHARGE_FLAG_DICT.THREE)
                    .set(ClinicOrders::getConfirmFlag, BaseConstants.CONFIRM_FLAG.RETURNED)
                    .eq(ClinicOrders::getId, prescriptionApplyVo.getOrdersId())
            );
            clinicOrdersCostMapper.update(null, Wrappers.<ClinicOrdersCost>lambdaUpdate()
                    .set(ClinicOrdersCost::getChargeFlag, BaseConstants.CHARGE_FLAG_DICT.THREE)
                    .set(ClinicOrdersCost::getConfirmFlag, BaseConstants.CONFIRM_FLAG.RETURNED)
                    .eq(ClinicOrdersCost::getOrdersId, prescriptionApplyVo.getOrdersId())
            );
            List<String> applyItemIds = prescriptionApplyVo.getPrescriptionItemVos().stream().map(PrescriptionItemVo::getItemId).collect(Collectors.toList());
            outpFeesDetailMapper.update(null, Wrappers.<OutpFeesDetail>lambdaUpdate()
                    .set(OutpFeesDetail::getChargeFlag, BaseConstants.CHARGE_FLAG_DICT.THREE)
                    .set(OutpFeesDetail::getConfirmFlag, BaseConstants.CONFIRM_FLAG.RETURNED)
                    .in(OutpFeesDetail::getApplyItemId, applyItemIds)
            );
        }
        return b;
    }
}
